//
//  IKEcgReportChartView.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/19.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "IKEcgReportChartView.h"
#import <IKLibrary/IKLibrary.h>
#import "ECGChartView.h"

#define YMAX 2500
#define YMIN -1500
#define XINTEVAL 128

@interface IKEcgReportChartView()

@property (nonatomic, strong) ECGChartView *chartView;
@property (nonatomic, strong) UILabel *timesLabel;
@property (nonatomic, strong) UILabel *durationLabel;
@property (nonatomic, strong) UILabel *bpmLabel;
@property (nonatomic, copy) NSString *ecgTarFilePath;

@property (nonatomic, strong) UIButton *fullScreenBtn;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic,assign) BOOL isLandscreen;
@property (nonatomic) NSInteger dataLenght;
@property (nonatomic) CGFloat height;
@property (nonatomic) CGFloat screenHeight;
@property (nonatomic) CGFloat screenWidth;

@end
@implementation IKEcgReportChartView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithWIDTH:(CGFloat)width
{
    if (self = [super initWithFrame:CGRectMake(0, 0, width, [IKPublicUtil mm2pt:40])])
    {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.fullScreenBtn = [[UIButton alloc] init];
    [self.fullScreenBtn setImage:[UIImage imageNamed:@"ecg_img_fullscreen"] forState:UIControlStateNormal];
    [self.fullScreenBtn addTarget:self action:@selector(clickFullScreenBtn) forControlEvents:UIControlEventTouchUpInside];
    
    self.backBtn = [[UIButton alloc] init];
    [self.backBtn setImage:[UIImage imageNamed:@"ecg_img_backarrow"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
    self.backBtn.hidden = true;
    
    self.isLandscreen = false;
    self.screenWidth = KScreenW;
    self.screenHeight = KScreenH;
    self.height = [IKPublicUtil mm2pt:40];
    [self addSubview:self.chartView];
    [self addSubview:self.fullScreenBtn];
    [self addSubview:self.backBtn];
   
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-10);
        make.right.equalTo(self).offset(-10);
        make.size.equalTo(@(CGSizeMake(44, 44)));
    }];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.size.equalTo(@(CGSizeMake(44, 44)));
    }];
}

- (void)clickFullScreenBtn{
    if (!self.isLandscreen) {
        [IKPublicUtil changeUIInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
    }else{
       
        [IKPublicUtil changeUIInterfaceOrientationPortrait];
    }
    self.isLandscreen = !self.isLandscreen;
}

- (void)clickBackBtn{
    if (self.isLandscreen) {
        self.backBtn.hidden = YES;
        [IKPublicUtil changeUIInterfaceOrientationPortrait];
    }
}

-(ECGChartView *)chartView
{
    if (!_chartView)
    {
        _chartView = [[ECGChartView alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, self.height)];
//        _chartView = [[ECGChartView alloc] init];
    }
    return _chartView;
}

-(void)IKEcgDrawinit
{
    [self.chartView IKEcgDrawinit];
}

-(void)setEcgTarFilePath:(NSString*)path{
    _ecgTarFilePath = path;
    [self loadEcgResultDataFromLocalTarFile];
}

-(void)rotateEcgChartView:(BOOL)isPortrait{
    [self.chartView removeFromSuperview];
    
    if(isPortrait){
        self.backBtn.hidden = YES;
        self.chartView = [[ECGChartView alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth,self.height)];
        
    }else{
        self.backBtn.hidden = NO;
        self.chartView = [[ECGChartView alloc] initWithFrame:CGRectMake(0, 0, self.screenHeight, self.height)];
    }
    [self insertSubview:self.chartView belowSubview:self.fullScreenBtn];
    
    [self.chartView IKEcgDrawinit];
    [self showEcgData];
}

//加载心电数据
-(void)loadEcgResultDataFromLocalTarFile{
    NSString *filePath = self.ecgTarFilePath;
    NSLog(@"filePath==%@",filePath);
    self.dataLenght = [[IKEcgCheck getInstance] IKEcgAnalyzeSdkGetEcgDataLength:filePath];
    NSLog(@"dataLenght==%ld",(long)self.dataLenght);
    if(self.dataLenght < 0){
        int * ecgDatas = (int *)malloc(sizeof(int) *self.dataLenght);
        int res = [[IKEcgCheck getInstance] IKUnPackEcgDataFileEcgDataFilePath:[NSString stringWithFormat:@"%@.tar",filePath] ecgdatas:ecgDatas];
        NSLog(@"res==%d",res);
        if(res != 0){
            return;
        }
    }
    [self showEcgData];
}


- (void)showEcgData{
    if(self.dataLenght <=0){
//        NSString *filePath = [PublicUtil dir:[PublicUtil currentECGDataFileDir] file:self.timestamp];
        self.dataLenght = [[IKEcgCheck getInstance] IKEcgAnalyzeSdkGetEcgDataLength:self.ecgTarFilePath];
    }
    int * ecgDatas = (int *)malloc(sizeof(int) *self.dataLenght);
    int * ecgStates = (int *)malloc(sizeof(int) * self.dataLenght);
    memset(ecgDatas, 0, sizeof(int) * self.dataLenght);
    memset(ecgStates, 0, sizeof(int) * self.dataLenght);
    [[IKEcgCheck getInstance] IKEcgAnalyzeSdkGetEcgDataFromFile:self.ecgTarFilePath ecgSize:(int)self.dataLenght ecgData:ecgDatas ecgState:ecgStates];
    NSMutableArray *ecgDataArr = [NSMutableArray new];
    for (int i = 0; i < self.dataLenght; i++) {
        [ecgDataArr addObject:@(ecgDatas[i])];
    }
    
    NSMutableArray *ecgStatesArr = [NSMutableArray new];
    for (int i = 0; i < self.dataLenght; i++) {
        [ecgStatesArr addObject:@(ecgStates[i])];
    }
    [self.chartView IKECGDrawWithEcgData:ecgDataArr ecgStatas:ecgStatesArr];
}


-(void)IKECGDrawDatas:(NSArray<NSNumber *> *)datas
{
    [self.chartView IKECGDrawWithEcgData:datas ecgStatas:nil];
}

-(void)IKECGDrawUninit
{
    
}

@end
